function findOrder(numCourses: number, prerequisites: number[][]): number[] {
  const to: Map<number, number[]> = new Map();
  const inDeg: number[] = new Array(numCourses).fill(0);
  for (let [ai, bi] of prerequisites) {
    to.has(bi) ? to.get(bi).push(ai) : to.set(bi, [ai]);
    inDeg[ai]++;
  }
  const queue: number[] = [];
  for (let i = 0; i < numCourses; i++) {
    if (inDeg[i] === 0) queue.push(i);
  }
  const lessons: number[] = [];
  while (queue.length) {
    const x = queue.shift();
    lessons.push(x);
    for (let y of to.get(x) || []) {
      inDeg[y]--;
      if (inDeg[y] === 0) {
        queue.push(y);
      }
    }
  }
  return lessons.length === numCourses ? lessons : [];
};